package com.superc.lib.network.implement.superp;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;

import javax.net.ServerSocketFactory;


import com.superc.lib.network.inteface.superp.IProtocolEventSink;
import com.superc.lib.network.inteface.superp.IServerHandler;

public class SuperServer implements Runnable ,IProtocolEventSink,IServerHandler{

	private ServerSocket socket=null;
	private int port=12345;
	private Thread serverThread=null;
	private IProtocolEventSink eventSink=null; 
	SuperProtocolHandler handler=null;
	
	public SuperServer(){
		
	}
	
	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public void start(){
		try {
			socket = ServerSocketFactory.getDefault().createServerSocket(port);
			serverThread=new Thread(this);
			serverThread.setName("ServerDaemonThread");
			serverThread.start();
		}  catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			
		}
	}

	@Override
	public void run() {
		while(true){
			try {
				Socket clientSocket=socket.accept();
				handler = new SuperProtocolHandler();
				handler.setEventSink(this);
				handler.accept(clientSocket);
			} catch (IOException e) {
				e.printStackTrace();
				break;
			}
		}
		
	}

	@Override
	public void ClientConnected(SocketAddress clientAddress,Boolean bolRejected,int handleId) {
		if(this.eventSink!=null) eventSink.ClientConnected(clientAddress, bolRejected,handleId);
	}

	@Override
	public void CommandReceived(byte commandId,String commandName, String parameter,int handleId) {
		if(this.eventSink!=null) eventSink.CommandReceived(commandId,commandName, parameter,handleId);
	}

	public void setEventSink(IProtocolEventSink sink) {
		this.eventSink=sink;
	}

	public IProtocolEventSink getEventSink() {
		return eventSink;
	}

	@Override
	public void close() {
		if(handler!=null) handler.close();
	}

	@Override
	public boolean getDataCompressed() {
		return false;
	}

	@Override
	public void setDataCompressed(boolean isCompressed) {
		
	}

	@Override
	public void sendCommand(Byte command, String parameter) throws IOException {
		if(handler!=null) handler.sendCommand(command, parameter);
	}

	@Override
	public void sendCommand(Byte command, String parameter, boolean compressed)
			throws IOException {
		if(handler!=null) handler.sendCommand(command, parameter,compressed);
	}

	@Override
	public void StreamDataReceived(byte[] datas,int startIndex, int dataLength, int handleId) {
		if(this.eventSink!=null) eventSink.StreamDataReceived(datas, startIndex,dataLength, handleId);
	}

	@Override
	public void sendStream(byte[] datas, int startIndex, int length ) throws IOException {
		if(handler!=null) handler.sendStream(datas, startIndex, length );
	}

	@Override
	public void sendStream(byte[] datas, int startIndex, int length,
			  boolean isCompressed) throws IOException {
		if(handler!=null) handler.sendStream(datas, startIndex, length, isCompressed);
	}

	@Override
	public void ConnectionClosed(int handleId) {
		if(eventSink!=null) eventSink.ConnectionClosed(handleId);
	}
	
	@Override
	public void ErrorHappened(Exception exp,int handleId) {
		if(eventSink!=null) eventSink.ErrorHappened(exp,handleId);
	}
}
